package com.xdap.common.dao;

import com.definesys.mpaas.query.MpaasQuery;
import com.xdap.common.pojo.po.ComApaasDataModel;
import com.xdap.runtime.service.RuntimeDatasourceService;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import java.util.List;


@Repository
@RequiredArgsConstructor
public class ComApaasDataModelDao {
    private final RuntimeDatasourceService runtimeDatasourceService;
    @Value("xdap_app_${apaas.single.tenantId}")
    private String tenantSchema;
    public static final String TABLE_NAME = "apaas_data_model";


    /**
     * @auth lishushu
     * @2024/3/18 10:46
     * @desc {获取可以点击修复数据的模型们}
     **/
    public List<ComApaasDataModel> queryNeedFixModel(List<String> modeCodeList) {
        MpaasQuery mpaasQuery = runtimeDatasourceService.buildBusinessMpaasQuery(tenantSchema, TABLE_NAME);
        mpaasQuery.table(TABLE_NAME);
        if (CollectionUtils.isNotEmpty(modeCodeList)) {
            mpaasQuery.in("model_code", modeCodeList);
        }

        // 只有版本为V2，并且是从数据源同步的模型需要做数据修复
        mpaasQuery.eq("api_version", "V2");
        mpaasQuery.eq("generate_type_method", "GENERATE_TYPE_METHOD_SYSTEM_TABLE");
        // 只处理状态是启动状态的数据
        mpaasQuery.eq("status", "ENABLE");
        mpaasQuery.eq("model_type", "APAAS_DATABASE");

        return mpaasQuery.doQuery(ComApaasDataModel.class);
    }
}
